//
//  SpecialDetailViewController.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "SpecialDetailViewController.h"
#import "HTTPMethod.h"
#import "changeString.h"
#import "FitHeader.h"
#import "CYNextSpecialTableViewCell.h"
#import "CYNextSpecialModel.h"
#import <MJRefresh.h>
#import "NextSpecialViewController.h"
#import "VideoListViewController.h"
@interface SpecialDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *dataArray;
@property(nonatomic, assign)NSInteger page;

@end

@implementation SpecialDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray = [NSMutableArray array];
    self.navigationController.navigationBar.translucent = YES;
    [self getData];
    self.view.backgroundColor = [UIColor whiteColor];
    //加载
    _page += 0;
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page += 10;
        [self getData];
    }];
}

-(void)getData
{
    NSString *PostString = @"http://api.haodou.com/index.php?appid=4&appkey=573bbd2fbd1a6bac082ff4727d952ba3&appsign=13d95a10898ce4c8f8746e7bcffe7382&channel=appstore&deviceid=0f607264fc6318a92b9e13c65db7cd3c%7C7C4F45AC-D834-40CC-8E57-1C73070C0250%7C28F136CD-C3C2-4542-AF9B-2BAD4F1067E6&format=json&loguid=&method=Info.getAlbumInfo&nonce=1446599298&sessionid=1446599275&signmethod=md5&timestamp=1446599298&uuid=8aa885128b339761cd4b5fc380d868d6&v=2&vc=43&vn=v5.4.0";
    NSString *bodyString = [NSString stringWithFormat:@"aid=%@&limit=10&offset=%ld&sign=&uid=&uuid=8aa885128b339761cd4b5fc380d868d6", _IdString, _page];
    [HTTPMethod getDataByString:PostString BodyDic:[changeString changeStringToDictionary:bodyString] WithDataBlock:^(id data) {
        
        NSDictionary *dic = [data objectForKey:@"result"];
        NSArray *array = [dic objectForKey:@"list"];
        if (array.count != 0) {
            for (NSDictionary *dataDic in array) {
                CYNextSpecialModel *special = [[CYNextSpecialModel alloc]init];
                [special setValuesForKeysWithDictionary:dataDic];
                [_dataArray addObject:special];
            }
        }
        [_tableView.footer endRefreshing];
        [_tableView reloadData];
    }];
}

-(void)loadView
{
    [super loadView];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
#pragma mark 自适应高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:13], NSFontAttributeName, nil];
    CGRect rect = [[_dataArray[indexPath.row]Intro] boundingRectWithSize:CGSizeMake(345 * wt, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return 200 * ht + rect.size.height;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    CYNextSpecialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[CYNextSpecialTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (_dataArray.count != 0) {
        cell.special = _dataArray[indexPath.row];
        [cell setDataString:[_dataArray[indexPath.row] Intro]];//自适应
    }
    //cell动画
    cell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        
        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
    }];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[[_dataArray[indexPath.row]HasVideo] stringValue] isEqualToString:@"0"]) {
        NextSpecialViewController *nvc = [[NextSpecialViewController alloc]init];
        nvc.tempModel = _dataArray[indexPath.row];
        [self.navigationController pushViewController:nvc animated:YES];
    }
    else
    {
        //跳转到视频
        VideoListViewController *vvc = [[VideoListViewController alloc]init];
        vvc.IdString = [[_dataArray[indexPath.row]RecipeId] stringValue];
        [self.navigationController pushViewController:vvc animated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
